package ;

import easelhx.display.Bitmap;
import easelhx.display.BitmapAnimation;
import easelhx.display.Graphics;
import easelhx.display.Shape;
import easelhx.display.SpriteSheet;
import easelhx.utils.Ticker;
import js.JQuery;
import js.Lib;
import preloadjs.PreloadJS;

/**
 * ...
 * @author 
 */

class SpriteSheetExample extends Example
{
	private var grant:BitmapAnimation;
	private var w:Int;
	private var h:Int;
	private var assets:Array<Dynamic>;
	private var ground:Shape;
	private var hill:Shape;
	private var hill2:Shape;
	private var sky:Shape;
	private var runningRate:Float;
	private var isInWarp:Bool;
	private var isStationary:Bool;
	private var stationaryPosition:Int;
	private var isPassed:Bool;

	public function new() 
	{
		super();
	}
	
	override public function run():Void 
	{
		super.run();
		
		new JQuery("body").prepend('<div id="loader"></div>');
		
		runningRate = 2.5;
		isInWarp = false;
		isStationary = false;
		stationaryPosition = 300;
		isPassed = false;

		var spriteSheet:Dynamic ={"animations": {"run": [0, 25], "jump": [26, 63]}, "images": ["assets/runningGrant.png"], "frames": {"regX": 0, "height": 292.5, "count": 64, "regY": 0, "width": 165.75}};

		var ss:SpriteSheet = new SpriteSheet(spriteSheet);
		grant = new BitmapAnimation(ss);

		// Set up looping
		ss.getAnimation("run").next = "run";
		ss.getAnimation("jump").next = "run";
		grant.gotoAndPlay("run");

		// Position the Grant sprite
		grant.x = -200;
		grant.y = 90;
		grant.scaleX = grant.scaleY = 0.8;

		// grab canvas width and height for later calculations:
		w = canvas.width;
		h = canvas.height;

		assets = new Array<Dynamic>();

		var manifest : Dynamic = [
			{src:"assets/runningGrant.png", id:"grant"},
			{src:"assets/sky.png", id:"sky"},
			{src:"assets/ground.png", id:"ground"},
			{src:"assets/parallaxHill1.png", id:"hill"},
			{src:"assets/parallaxHill2.png", id:"hill2"}
		];
		
		var loader : PreloadJS = new PreloadJS();
		loader.useXHR = false;  // XHR loading is not reliable when running locally.
		loader.onFileLoad = handleFileLoad;
		loader.onComplete = handleComplete;
		loader.onError = handleError;
		loader.loadManifest(manifest);
		stage.autoClear = false;
		
		//untyped console.dir(manifest);
		
	}
	
	private function handleError(event) : Void 
	{
		Lib.alert("handleError");
	}
	
	private function handleFileLoad(event) : Void
	{
		assets.push(event);
		//untyped console.dir(assets);
	}

	private function handleComplete() : Void
	{
		for (i in 0...assets.length) 
		{
			
			var item = assets[i]; //loader.getResult(id);
			var id = item.id;
			var result = item.result;

			if (item.type == PreloadJS.IMAGE) {
				var bmp:Bitmap = new Bitmap(result);
			}
			switch (id) {
				case "sky":
					sky = new Shape(new Graphics().beginBitmapFill(result).drawRect(0,0,w,h));
				case "ground":
					ground = new Shape();
					var g = ground.graphics;
					g.beginBitmapFill(result);
					g.drawRect(0, 0, w+330, 79);
					ground.y = h-259;
				case "hill":
					hill = new Shape(new Graphics().beginBitmapFill(result).drawRect(0,0,282,59));
					hill.x = Math.random() * w;
					hill.scaleX = hill.scaleY = 3;
					hill.y = 144;
				case "hill2":
					hill2 = new Shape(new Graphics().beginBitmapFill(result).drawRect(0,0,212,50));
					hill2.x = Math.random() * w;
					hill2.scaleX = hill2.scaleY = 3;
					hill2.y = 171;
			}
		}

		Lib.document.getElementById("loader").className = "";

		if (grant == null) {
			//untyped console.log("Can not play. Grant sprite was not loaded.");
			return;
		}

		stage.addChild(sky);
		stage.addChild(ground);
		stage.addChild(hill);
		stage.addChild(hill2);
		stage.addChild(grant);
		stage.onMouseDown = handleJumpStart;

		Ticker.setFPS(40);
		Ticker.addListener(this);
	}

	private function handleJumpStart() : Void
	{
		grant.gotoAndPlay("jump");
	}

	private function tick() : Void
	{
		var outside = w + 20;
		var position = grant.x + runningRate;
		grant.x = (position >= outside) ? -200 : position;

		ground.x = (ground.x-15) % 330;
		hill.x = (hill.x - 0.8);
		if (hill.x + 838 <= 0) { hill.x = outside; }
		hill2.x = (hill2.x - 1.2);
		if (hill2.x + 633 <= 0) { hill2.x = outside; }

		stage.update();
	}
	
}